import 'package:flutter/material.dart';
import '../model/bgcContentSave.dart';
import '../model/bgcContentBo.dart';
import '../model/draftModel.dart';
import '../services/contentService.dart';

class ContentProvider with ChangeNotifier {
  final ContentService _contentService = ContentService();

  // 发布相关状态
  bool _isPublishing = false;
  String? _publishError;
  BgcContentBo? _publishedContent;
  int? _taskRecordId;

  // 草稿相关状态
  bool _isSavingDraft = false;
  String? _draftError;
  DraftModel? _savedDraft;

  // 获取草稿相关状态
  DraftModel? _draftData;
  bool _isLoadingDraft = false;
  String? _getDraftError;

  // 发布相关 getters
  bool get isPublishing => _isPublishing;
  String? get publishError => _publishError;
  BgcContentBo? get publishedContent => _publishedContent;

  // 草稿相关 getters
  bool get isSavingDraft => _isSavingDraft;
  String? get draftError => _draftError;
  DraftModel? get savedDraft => _savedDraft;

  // 获取草稿相关 getters
  DraftModel? get draftData => _draftData;
  bool get isLoadingDraft => _isLoadingDraft;
  String? get getDraftError => _getDraftError;

  // 总体状态 getter - 如果正在发布或保存草稿都算作忙碌状态
  bool get isLoading => _isPublishing || _isSavingDraft || _isLoadingDraft;

  // 保存发布后返回的taskRecordId
  int get taskRecordId => _taskRecordId ?? 0;

  // 发布内容
  Future<void> publishContent(BgcContentSave contentSave) async {
    _isPublishing = true;
    _publishError = null;
    notifyListeners();

    try {
      _publishedContent = await _contentService.publishContent(contentSave);
      _taskRecordId = _publishedContent?.taskRecordId;
      _publishError = null;
    } catch (e) {
      _publishError = e.toString();
      _publishedContent = null;
    } finally {
      _isPublishing = false;
      notifyListeners();
    }
  }

  // 保存草稿
  Future<void> saveDraft(BgcContentSave contentSave) async {
    _isSavingDraft = true;
    _draftError = null;
    notifyListeners();

    try {
      _savedDraft = await _contentService.saveDraft(contentSave);
      _draftError = null;
    } catch (e) {
      _draftError = e.toString();
      _savedDraft = null;
    } finally {
      _isSavingDraft = false;
      notifyListeners();
    }
  }

  // 更新草稿
  Future<void> updateDraft(BgcContentSave contentSave) async {
    _isSavingDraft = true;
    _draftError = null;
    notifyListeners();

    try {
      bool updateSuccess = await _contentService.updateDraft(contentSave);
      if (updateSuccess) {
        _draftError = null;
        print('草稿更新成功');
        // 更新草稿时，我们不需要更新_savedDraft，因为我们已经有currentDraftId了
      } else {
        throw Exception('更新草稿失败');
      }
    } catch (e) {
      _draftError = e.toString();
      print('草稿更新失败: $e');
    } finally {
      _isSavingDraft = false;
      notifyListeners();
    }
  }

  // 清除发布错误
  void clearPublishError() {
    _publishError = null;
    notifyListeners();
  }

  // 清除草稿错误
  void clearDraftError() {
    _draftError = null;
    notifyListeners();
  }

  // 重置所有状态
  void reset() {
    _isPublishing = false;
    _publishError = null;
    _publishedContent = null;
    _isSavingDraft = false;
    _draftError = null;
    _savedDraft = null;
    notifyListeners();
  }

  // 发布草稿（使用新的API）
  Future<void> publishDraftWithData(BgcContentSave contentSave) async {
    _isPublishing = true;
    _publishError = null;
    notifyListeners();

    try {
      _publishedContent = await _contentService.publishDraft(contentSave);
      _taskRecordId = _publishedContent?.taskRecordId;
      _publishError = null;
      print('草稿发布成功: ${_publishedContent?.id}');
    } catch (e) {
      _publishError = e.toString();
      _publishedContent = null;
      print('草稿发布失败: $e');
    } finally {
      _isPublishing = false;
      notifyListeners();
    }
  }

  // 根据任务记录ID获取草稿
  Future<void> getDraftByTaskRecordId(int taskRecordId) async {
    _isLoadingDraft = true;
    _getDraftError = null;
    _draftData = null;
    notifyListeners();

    try {
      _draftData = await _contentService.getDraftByTaskRecordId(taskRecordId);
      _getDraftError = null;
      print('草稿获取成功: ${_draftData?.id}');
    } catch (e) {
      _getDraftError = e.toString();
      _draftData = null;
      print('草稿获取失败: $e');
    } finally {
      _isLoadingDraft = false;
      notifyListeners();
    }
  }

  // 清除草稿数据
  void clearDraftData() {
    _draftData = null;
    _getDraftError = null;
    _isLoadingDraft = false;
    notifyListeners();
  }
}
